package com.kermitye.treenodelib

import java.util.*

object TreeNodeHelper {
    fun <T> getExpendedChildren(parent: TreeNode<T>): List<TreeNode<T>> {
        val childrenList: MutableList<TreeNode<T>> = arrayListOf()
        val children = parent.children
        if (children != null) {
            for (child in children) {
                childrenList.add(child)
                if (child.isExpand) {
                    childrenList.addAll(getExpendedChildren(child))
                }
            }
        }
        return childrenList
    }

    fun <T> expandAll(parentList: List<TreeNode<T>>?) {
        parentList?.forEach {
            it.isExpand = true
            expandAll(it.children)
        }
    }

    fun <T> collapseAll(parentList: List<TreeNode<T>>?) {
        parentList?.forEach {
            it.isExpand = false
            collapseAll(it.children)
        }
    }

    fun <T> expandLevel(parentList: List<TreeNode<T>>?, level: Int) {
        parentList?.forEach {
            if (it.getLevel() < level) {
                it.isExpand = true
                expandLevel(it.children, level)
            }
        }
    }
}